package com.hdd.starter.listener;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.hdd.starter.annotation.CanalEventListener;
import com.hdd.starter.annotation.ddl.AlertTableListenPoint;
import com.hdd.starter.annotation.ddl.CreateIndexListenPoint;
import com.hdd.starter.annotation.ddl.CreateTableListenPoint;
import com.hdd.starter.annotation.ddl.DropTableListenPoint;
import com.hdd.starter.annotation.dml.DeleteListenPoint;
import com.hdd.starter.annotation.dml.InsertListenPoint;
import com.hdd.starter.annotation.dml.UpdateListenPoint;
import com.hdd.starter.core.CanalMsg;
import com.hdd.starter.util.BeanUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

/**
 * 注解方法
 */
@CanalEventListener
public class IAnnoEventListener {

	private static Logger logger  = LoggerFactory.getLogger(IAnnoEventListener.class);
	private static BeanUtil beanUtil = new BeanUtil();

	@Resource
	QueryRunner queryRunner;


	@InsertListenPoint
	public void onEventInsertData(CanalMsg canalMsg, CanalEntry.RowChange rowChange) {
		List<CanalEntry.RowData> rowDatasList = rowChange.getRowDatasList();
		for (CanalEntry.RowData rowData : rowDatasList) {
//			String sql = "use " + canalMsg.getSchemaName() + ";\n";
			String sql ="";
			StringBuffer colums = new StringBuffer();
			StringBuffer values = new StringBuffer();
			rowData.getAfterColumnsList().forEach((c) -> {
				colums.append(c.getName() + ",");
				values.append("'" + c.getValue() + "',");
			});
			sql += "INSERT INTO " + canalMsg.getTableName() + "(" + colums.substring(0, colums.length() - 1) + ") VALUES(" + values.substring(0, values.length() - 1) + ");";

			try {
				int row = queryRunner.update(sql);
			}catch (SQLException e){
				e.printStackTrace();
				logger.error("新增,{}",e);
			}

			logger.info("数据新增,{}",sql);
		}
	}

	@UpdateListenPoint
	public void onEventUpdateData(CanalMsg canalMsg, CanalEntry.RowChange rowChange) {
		List<CanalEntry.RowData> rowDatasList = rowChange.getRowDatasList();
		for (CanalEntry.RowData rowData : rowDatasList) {

			String sql ="";

//			String sql = "use " + canalMsg.getSchemaName() + ";\n";
			StringBuffer updates = new StringBuffer();
			StringBuffer conditions = new StringBuffer();
			rowData.getAfterColumnsList().forEach((c) -> {
				if (c.getIsKey()) {
					conditions.append(c.getName() + "='" + c.getValue() + "'");
				} else {
					updates.append(c.getName() + "='" + c.getValue() + "',");
				}
			});
			sql += "UPDATE " + canalMsg.getTableName() + " SET " + updates.substring(0, updates.length() - 1) + " WHERE " + conditions;
			try {
				queryRunner.update(sql);
			}catch (SQLException e){
				e.printStackTrace();
				logger.error("更新,{}",e);
			}
			logger.info("数据更新,{}",sql);
		}
	}

	@DeleteListenPoint
	public void onEventDeleteData(CanalEntry.RowChange rowChange, CanalMsg canalMsg) {

		List<CanalEntry.RowData> rowDatasList = rowChange.getRowDatasList();
		for (CanalEntry.RowData rowData : rowDatasList) {

			if (!CollectionUtils.isEmpty(rowData.getBeforeColumnsList())) {
			//如果 存在  true  不存在 false

				String sql ="";
//				String sql = "use " + canalMsg.getSchemaName() + ";\n";
				sql += "DELETE FROM " + canalMsg.getTableName() + " WHERE ";
				StringBuffer idKey = new StringBuffer();
				StringBuffer idValue = new StringBuffer();
				for (CanalEntry.Column c : rowData.getBeforeColumnsList()) {
					if (c.getIsKey()) {
						idKey.append(c.getName());
						idValue.append(c.getValue());
						break;
					}
				}
				sql += idKey + " =" + idValue + ";";
				try {
					queryRunner.update(sql);
				}catch (SQLException e){
					e.printStackTrace();
					logger.error("删除,{}",e);
				}
				logger.info("数据删除,{}",sql);
			}

		}
	}

	@CreateTableListenPoint
	public void onEventCreateTable(CanalEntry.RowChange rowChange) {
		String sql ="";
		sql =  rowChange.getSql();
		//String sql = "use " + rowChange.getDdlSchemaName() + ";\n" + rowChange.getSql();
		try {
			queryRunner.update(sql);
		}catch (SQLException e){
			e.printStackTrace();
			logger.error("创建表,{}",e);
		}
		logger.info("创建表操作,{}",sql);
	}

	@DropTableListenPoint
	public void onEventDropTable(CanalEntry.RowChange rowChange) {
		String sql ="";
		sql =  rowChange.getSql();
		//String sql = "use " + rowChange.getDdlSchemaName() + ";\n" + rowChange.getSql();
		try {
			queryRunner.update(sql);
		}catch (SQLException e){
			e.printStackTrace();
			logger.error("删除表,{}",e);
		}
		logger.info("删除表操作,{}",sql);
	}

	@AlertTableListenPoint
	public void onEventAlertTable(CanalEntry.RowChange rowChange) {

		String sql ="";
		sql =  rowChange.getSql();
		//String sql = "use " + rowChange.getDdlSchemaName() + ";\n" + rowChange.getSql();
		try {
			queryRunner.update(sql);
		}catch (SQLException e){
			e.printStackTrace();
			logger.error("修改表信息,{}",e);
		}
		logger.info("修改表信息操作,{}",sql);
	}

	@CreateIndexListenPoint
	public void onEventCreateIndex(CanalMsg canalMsg, CanalEntry.RowChange rowChange){
		String sql ="";
		sql =  rowChange.getSql();
		//String sql = "use " + rowChange.getDdlSchemaName() + ";\n" + rowChange.getSql();
		try {
			queryRunner.update(sql);
		}catch (SQLException e){
			e.printStackTrace();
			logger.error("创建索引,{}",e);
		}
		logger.info("创建索引操作,{}",sql);

	}
	
	
}
